/**
 * @author hdeom
 * @date 2014. 1. 15.
 *
 */

package h.d.bluetooth.visitor.db;

import java.util.ArrayList;

import h.d.bluetooth.visitor.common.VaE;
import h.d.bluetooth.visitor.sort.SortData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author hdeom
 * @date 2014. 1. 15.
 *
 */
public class DBControl {

	public Context mContext;

	public String TAG = "DBControl";

	public static BVDBHelper dbHelper;


	public DBControl(Context context){
		mContext = context;

		dbHelper = new BVDBHelper(context);

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		dbHelper.init(db);
	}

	public long insertDeviceInfo(SortData s){

		long ret = 0;

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put(BVDBHelper.COL_INFO_NAME, s.getName());
		values.put(BVDBHelper.COL_INFO_FIXNAME, s.getFixName());
		values.put(BVDBHelper.COL_INFO_MACADDRESS, s.getMacAddress());

		ret = db.insert(BVDBHelper.TABLE_DEVICE_INFO, null, values);

		return ret;
	}

	public long insertDeviceLog(SortData s){

		long ret = 0;

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put(BVDBHelper.COL_INFO_DATE, s.getTime());
		values.put(BVDBHelper.COL_INFO_NAME, s.getName());
		values.put(BVDBHelper.COL_INFO_MACADDRESS, s.getMacAddress());
		values.put(BVDBHelper.COL_INFO_RSSI, s.getRssi());

		ret = db.insert(BVDBHelper.TABLE_DEVICE_LOG, null, values);

		return ret;
	}

	public long updateDeviceInfo(SortData s){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		long ret = 0;
		ContentValues values = new ContentValues();
		try {
			String where = BVDBHelper.COL_INFO_MACADDRESS + " = '"+s.getMacAddress()+"'";
			values.put(BVDBHelper.COL_INFO_NAME, s.getName());
			values.put(BVDBHelper.COL_INFO_FIXNAME, s.getFixName());
			values.put(BVDBHelper.COL_INFO_MACADDRESS, s.getMacAddress());
			ret = db.update(BVDBHelper.TABLE_DEVICE_INFO, values, where, null);

		}
		catch(Exception e)
		{
			if(VaE.HD) Log.d(TAG, "Caught an exception while getting pattern based results: " + e);
		}
		return ret;
	}

	public long updateDeviceLog(SortData s){
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		long ret = 0;
		ContentValues values = new ContentValues();
		if(VaE.HD) Log.d("test_data", "readerId : ");
		try {
			String where = BVDBHelper.COL_INFO_MACADDRESS + " = '"+s.getMacAddress()+"'";
			values.put(BVDBHelper.COL_INFO_DATE, s.getTime());
			values.put(BVDBHelper.COL_INFO_NAME, s.getName());
			values.put(BVDBHelper.COL_INFO_MACADDRESS, s.getMacAddress());
			values.put(BVDBHelper.COL_INFO_RSSI, s.getRssi());
			ret = db.update(BVDBHelper.TABLE_DEVICE_LOG, values, where, null);

		}
		catch(Exception e)
		{
			if(VaE.HD) Log.d(TAG, "Caught an exception while getting pattern based results: " + e);
		}
		return ret;
	}

	public long deleteDeviceInfo(SortData s){
		long ret = 0;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String where = BVDBHelper.COL_INFO_MACADDRESS + "= '" + s.getMacAddress() + "'";
		db.delete(BVDBHelper.TABLE_DEVICE_INFO, where, null);
		return ret;
	}

	public long deleteDeviceLog(SortData s){
		long ret = 0;
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String where = BVDBHelper.COL_INFO_MACADDRESS + "= '" + s.getMacAddress() + "'";
		db.delete(BVDBHelper.TABLE_DEVICE_LOG, where, null);
		return ret;
	}


	public ArrayList<SortData> quiryDeviceInfoAll(){
		ArrayList<SortData> sAll = new ArrayList<SortData>();

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		Cursor c = db.query(BVDBHelper.TABLE_DEVICE_INFO, null, null, null, null, null, null);

		while(c.moveToNext()){

			SortData s = new SortData();

			s.setName(c.getString(c.getColumnIndex(BVDBHelper.COL_INFO_NAME)));
			s.setFixName(c.getString(c.getColumnIndex(BVDBHelper.COL_INFO_FIXNAME)));
			s.setMacAddress(c.getString(c.getColumnIndex(BVDBHelper.COL_INFO_MACADDRESS)));

			sAll.add(s);
		}

		c.close();


		return sAll;

	}




}
